package com.itheima.health.service.impl;


import com.alibaba.dubbo.config.annotation.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author LYK
 * @Date 2020/2/21  22:10
 * @Version v1.0
 * @Description
 */
@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

    //依赖注入
    @Autowired
    private PermissionDao permissionDao;

    /**
     * 修改数据
     * @param permission
     */
    @Override
    public void update(Permission permission) {
        permissionDao.update(permission);
    }

    @Override
    public void delete(Integer id) {
        //查询与权限关联的表是否有数据
     Long count = permissionDao.findDeleteById(id);
     if (count>0){
         //表示有数据关联不能删除
         throw new RuntimeException("权限关联的有角色信息，不能删除!");
     }
     //没有关联数据就可以删除
        permissionDao.delete(id);
    }

    /**
     * 数据添加
     * @param permission
     */
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    /**
     * 数据回显
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //基于条件进行查询
        Page<Permission> rolePage = permissionDao.findPage(queryPageBean.getQueryString());
        //封装并返回
        PageResult pageResult = new PageResult(rolePage.getTotal(), rolePage.getResult());
        return pageResult;

    }

    /**
     * 查询权限集合
     * @return
     */
    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }
}
